<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use Carbon\Carbon;
use think\Db;
use think\Model;
class Discount extends Model
{
    // 活动
    protected $name = '_qt_data_activity';
    protected $autoWriteTimestamp = true;

    /**
     * 获取活动数据
     * @param $getPost
     * @return array
     */
    public static function getDiscount($getPost){
        $activeData = Db::name('qt_data_activity')->where(['id'=>$getPost['active_id']])->find();
        if(empty($activeData)){
            return ['code'=> 204,'msg'=>'无效的活动'] ;
        }
        //比较是否在日期内
        if($activeData['date_start'] != 0){
            if($activeData['date_start'] > time() || time() > $activeData['date_end']){
                $result['project'] = [];
                $result['goods'] = [];
                echo '111';
                return ['code'=> 200,'msg'=>'查询成功','data'=>$result];
            }
        }
        //比较是否在时间段内
        if($activeData['time_start'] != '' || $activeData['time_end'] != ''){
            $time = date('H:i:s');
            if($time < $activeData['time_start'] || $time > $activeData['time_end']){
                $result['project'] = [];
                $result['goods'] = [];
                return ['code'=> 200,'msg'=>'查询成功','data'=>$result];
            }
        }
        //项目
        if (!empty($getPost['project'])) {
            $projectRes = json_decode($getPost['project'], true);
            foreach ($projectRes as $key => $value) {
                //查询项目是否有活动价
                $dis = Db::name('qt_data_activity_item')
                    ->where(['item_type' => 'project', 'item_id' => $value['project_id'], 'activity_id' => $getPost['active_id']])
                    ->find();
                if (!empty($dis)) {
                    $projectRes[$key]['activity_price'] = $dis['activity_price'];
                    $projectRes[$key]['add_block_price'] = $dis['add_block_price'];
                    if (isset($getPost['viper_card_type_id']) && !empty($getPost['viper_card_type_id'])) {
                        $vip = Db::name('qt_data_activity_item_viper')->where(['activity_item_id' => $dis['id'], 'viper_card_type_id' => $getPost['viper_card_type_id']])->find();
                    }else{
                        $vip = [];
                    }
                    if (empty($vip)) {
                        $viper_price = $dis['activity_price'];
                        $viper_add_block_price = '';
                    } else {
                        $viper_price = $vip['viper_price'];
                        $viper_add_block_price = $vip['viper_add_block_price'];
                        $projectRes[$key]['activity_price'] = $viper_price;
                        $projectRes[$key]['add_block_price'] = $viper_add_block_price;
                    }
                    $projectRes[$key]['activity_price'] = $viper_price;
                    $projectRes[$key]['viper_add_block_price'] = $viper_add_block_price;
                } else {
                    $projectRes[$key]['activity_price'] = $value['price'];
                    $projectRes[$key]['add_block_price'] = '';
                    $projectRes[$key]['viper_price'] = $value['price'];
                    $projectRes[$key]['viper_add_block_price'] = '';
                }

            }
        } else {
            $projectRes = [];
        }
        //商品
        if ($getPost['goods'] != '') {
            $goodsRes = json_decode($getPost['goods'], true);

            foreach ($goodsRes as $key => $value) {
                //查询项目是否有活动价
                $dis = Db::name('qt_data_activity_item')
                    ->where(['item_type' => 'goods', 'item_id' => $value['goods_id'], 'activity_id' => $getPost['active_id']])
                    ->find();
                if (!empty($dis)) {
                    if (isset($getPost['viper_card_type_id']) && !empty($getPost['viper_card_type_id'])) {
                        $vip = Db::name('qt_data_activity_item_viper')->where(['activity_item_id' => $dis['id'], 'viper_card_type_id' => $getPost['viper_card_type_id']])->find();
                    }else{
                        $vip = [];
                    }
                    if (empty($vip)) {
                        $viper_price = $dis['activity_price'];
                        $viper_add_block_price = '';
                    } else {
                        $viper_price = $vip['viper_price'];
                        $viper_add_block_price = $vip['viper_add_block_price'];
                        $goodsRes[$key]['activity_price'] = $viper_price;
                        $goodsRes[$key]['add_block_price'] = $viper_add_block_price;
                    }
                    $goodsRes[$key]['activity_price'] = $viper_price;
                    $goodsRes[$key]['viper_add_block_price'] = $viper_add_block_price;
                } else {
                    $goodsRes[$key]['activity_price'] = $value['goods_price'];
                    $goodsRes[$key]['add_block_price'] = '';
                }
            }
        } else {
            $goodsRes = [];
        }
        $result['project'] = $projectRes;
        $result['goods'] = $goodsRes;
        return ['code'=> 200,'msg'=>'查询成功','data'=>$result];
    }


    /**
     * 获取会员卡活动数据
     * @param $getPost
     * @return array
     */
    public static function getVipDiscount($getPost){
        //项目
        if ($getPost['project'] != '') {
            $projectRes = json_decode($getPost['project'], true);
            foreach ($projectRes as $key => $value) {
                //查询项目是否有活动价
                $dis = Db::name('qt_viper_activity_project')
                    ->where(['project_id' => $value['project_id'], 'viper_type_id' => $getPost['viper_type_id']])
                    ->find();
                if (!empty($dis)) {
                    //判断是否有会员价
                    if($dis['type'] != null){
                        $projectRes[$key]['activity_price'] = $dis['viper_price'];
                        if($dis['rule'] == '2'){
                            $time = time();
                        }else{
                            $time  = Db::name('qt_order_project')->where(['id'=>$value['project_order_id']])->value('created_time');
                        }
                        //存在时段会员价
                        if($dis['type'] == '1'){
                            //每天
                            $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                            $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                            if($startTime < $time && $time < $endTime) {
                                if($value['order_type'] == '1'){
                                    $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                }elseif($value['order_type'] == '5'){
                                    $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                }
                            }
                        }
                        if($dis['type'] == '2'){
                            //每周星期几
                            $value = explode(',',$dis['value']);
                            $week  = Carbon::now()->dayOfWeek;
                            if(in_array($week,$value)){
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time < $endTime){
                                    if($value['order_type'] == '1'){
                                        $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                    }elseif($value['order_type'] == '5'){
                                        $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                    }
                                }
                            }

                        }
                        if($dis['type'] == '3'){
                            //每月几天
                            $value = explode(',',$dis['value']);
                            $day = date('d');
                            if(in_array($day,$value)){
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time < $endTime){
                                    if($value['order_type'] == '1'){
                                        $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                    }elseif($value['order_type'] == '5'){
                                        $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                    }
                                }
                            }
                        }
                    }else{
                        $projectRes[$key]['activity_price'] = $dis['viper_price'];
                        if($value['order_type'] == '5'){
                            $projectRes[$key]['activity_price'] = $dis['add_block_price'];
                        }
                    }
                } else {
                    $projectRes[$key]['activity_price'] = $value['price'];
                    if($value['order_type'] == '2'){
                        $projectRes[$key]['activity_price'] = $dis['add_block_price'];
                    }
                }
            }
        } else {
            $projectRes = [];
        }
        //商品
        if ($getPost['goods'] != '') {
            $goodsRes = json_decode($getPost['goods'], true);
            foreach ($goodsRes as $key => $value) {
                //查询项目是否有活动价
                $dis = Db::name('qt_viper_activity_goods')
                    ->where(['product_id' => $value['goods_id'], 'viper_type_id' => $getPost['viper_type_id']])
                    ->find();
                if (!empty($dis)) {
                    //判断是否有会员价
                    if($dis['type'] != null){
                        $goodsRes[$key]['activity_price'] = $dis['viper_price'];
                        if($dis['rule'] == '2'){
                            $time = time();
                        }else{
                            $time  = Db::name('qt_product_order_item')->where(['id'=>$value['product_order_id']])->value('create_time');
                        }
                        //存在时段会员价
                        if($dis['type'] == '1'){
                            //每天
                            $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                            $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                            if($startTime < $time && $time < $endTime){
                                $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                            }
                        }
                        if($dis['type'] == '2'){
                            //每周星期几
                            $value = explode(',',$dis['value']);
                            $week  = Carbon::now()->dayOfWeek;
                            if(in_array($week,$value)){
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time< $endTime){
                                    $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                                }
                            }

                        }
                        if($dis['type'] == '3'){
                            //每月几天
                            $value = explode(',',$dis['value']);
                            $day = date('d');
                            if(in_array($day,$value)){
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time < $endTime){
                                    $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                                }
                            }
                        }
                    }else{
                        $goodsRes[$key]['activity_price'] = $dis['viper_price'];
                    }
                } else {
                    $goodsRes[$key]['activity_price'] = $value['goods_price'];
                }
            }
        } else {
            $goodsRes = [];
        }
        $result['project'] = $projectRes;
        $result['goods'] = $goodsRes;
        return ['code'=> 200,'msg'=>'查询成功','data'=>$result];
    }


}